//
//  ViewController.m
//  Shake
//
//  Created by YYY on 2017/12/7.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "ViewController.h"

#import "AudioTool.h"
#import <AVFoundation/AVFoundation.h>

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@property (nonatomic, strong) UIImageView *upImage;       //背景图片

@property (nonatomic, strong) UIImageView *downImage;     //上一半手 的图片

@property (nonatomic, strong) UIView *upView;             //下一半手

@property (nonatomic, strong) UIView *downView;           //下一半手 的图片

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //初始化子视图
    [self setUpSubViews];
}

//初始化子视图
- (void)setUpSubViews
{
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, kHeight / 4, kWidth, kHeight / 2)];
    image.image = [UIImage imageNamed:@"ShakeHideImg_women@2x.png"];
    [self.view addSubview:image];
    
    _upView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight / 2)];
    _upView.backgroundColor = [UIColor blackColor];
    
    _upImage = [[UIImageView alloc]initWithFrame:CGRectMake(50, kHeight / 4, kWidth - 100, kHeight / 4)];
    _upImage.image = [UIImage imageNamed:@"Shake_Logo_Up@2x.png"];
    
    [_upView addSubview:_upImage];
    [self.view addSubview:_upView];
    
    
    
    _downView = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight / 2, kWidth, kHeight / 2)];
    _downView.backgroundColor = [UIColor blackColor];
    
    _downImage = [[UIImageView alloc]initWithFrame:CGRectMake(50, 0, kWidth - 100, kHeight / 4)];
    _downImage.image = [UIImage imageNamed:@"Down@2x.png"];
    
    [_downView addSubview:_downImage];
    [self.view addSubview:_downView];
}

#pragma mark - 开始摇晃就会调用
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //开始摇晃 设置动画
    [UIView animateWithDuration:1 animations:^{
        
        _upView.frame = CGRectMake(0, -(kHeight / 4), kWidth, kHeight / 2);
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1 animations:^{
            
            _upView.frame = CGRectMake(0, 0, kWidth, kHeight / 2);
            
        }];
    }];
    
    [UIView animateWithDuration:1 animations:^{
        
        _downView.frame = CGRectMake(0, kHeight / 2 + kHeight / 4, kWidth, kHeight / 2);
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1 animations:^{
            
            _downView.frame = CGRectMake(0, kHeight / 2, kWidth, kHeight / 2);
            
        }];
        
    }];
    //播放摇晃声音
    [AudioTool playMusic:@"1.mp3"];
}

#pragma mark - 摇晃结束就会调用
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    //摇晃结束
}

#pragma mark - 摇晃被打断就会调用
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //摇晃被打断
}


@end
